<?php
namespace app\common\model;
use think\Cache;

/**
 * 系统设置模型
 */
class Setting extends BaseModel
{
    protected $name = 'setting';
    protected $createTime = false;

    /**
     * 获取器: 转义数组格式
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }

    /**
     * 获取指定项设置
     */
    public static function getItem($key, $wxapp_id = null)
    {
        $data = self::getAll($wxapp_id);
        return isset($data[$key]) ? $data[$key]['values'] : [];
    }

    /**
     * 获取设置项信息
     */
    public static function detail($key)
    {
        return self::get(compact('key'));
    }

    /**
     * 全局缓存: 系统设置
     */
    public static function getAll($wxapp_id = null)
    {
        $self = new static;
        is_null($wxapp_id) && $wxapp_id = $self::$wxapp_id;
        if (!$data = Cache::get('setting_' . $wxapp_id)) {
            $data = array_column(collection($self::all())->toArray(), null, 'key');
            Cache::set('setting_' . $wxapp_id, $data);
        }
        return array_merge_multiple($self->defaultData(), $data);
    }

    /**
     * 默认配置
     */
    public function defaultData()
    {
        return [
            'store' => [
                'key' => 'store',
                'describe' => '站点设置',
                'values' => ['name' => '河马云店'],
            ],
			'mode' => [
                'key' => 'mode',
                'describe' => '功能设置',
                'values' => [
                    1 => [
                        'is_open' => 1,
                        'name' => '店内就餐'
                    ],
					2 => [
                        'is_open' => 1,
                        'name' => '打包自提'
                    ],
					3 => [
                        'is_open' => 1,
                        'name' => '外卖直达'
                    ],
					4 => [
                        'is_open' => 1,
                        'name' => '预约订座'
                    ],
					5 => [
                        'is_open' => 1,
                        'name' => '排队取号'
                    ],
                ]
            ],
            'trade' => [
                'key' => 'trade',
                'describe' => '交易设置',
                'values' => [
                    'order' => [
                        'close_days' => '10',
                        'receive_days' => '10',
                        'refund_days' => '0'
                    ],
                    'freight_rule' => '10',
                ]
            ],
			'delivery' => [
                'key' => 'delivery',
                'describe' => '配送设置',
                'values' => [
					'delivery_range' => '5000',	//配送范围
					'free_range' => '0',		//免费配送范围
                    'delivery_price' => '0',	//配送费用
					'min_price' => '10',		//起送价格
					'dada' =>[],
                ]
            ],
            'storage' => [
                'key' => 'storage',
                'describe' => '上传设置',
                'values' => [
                    'default' => 'local',
                    'engine' => [
                        'qiniu' => [
                            'bucket' => '',
                            'access_key' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                    ]
                ],
            ],
            'sms' => [
                'key' => 'sms',
                'describe' => '短信通知',
                'values' => [
                    'default' => 'aliyun',
                    'engine' => [
                        'aliyun' => [
                            'AccessKeyId' => '',
                            'AccessKeySecret' => '',
                            'sign' => '河马科技',
                            'order_pay' => [
                                'is_enable' => '0',
                                'template_code' => '',
                                'accept_phone' => '',
                            ],
                        ],
                    ],
                ],
            ],
			'tpl' => [
                'key' => 'tpl',
                'describe' => '订阅消息',
                'values' => [
                    'finish' => '',  //订单完成通知
                    'take' => '',  //取餐提醒
                    'remind' => '',  //送餐进度提醒
                    'delivery' => '',  //订单配送通知
                ],
            ],
			'printer' => [
                'key' => 'printer',
                'describe' => '打印设置',
                'values' => [
					'title' => '河马云店',	//票据抬头
                    'is_open' => 0,	//是否开启
					'p_n' => 1,	//打印份数
					'pay_p' => 1,	//支付后打印
                ],
            ],
			'recharge' => [
                'key' => 'recharge',
                'describe' => '充值设置',
                'values' => [
                    'is_open' => 0,		//是否开启
					'is_custom' => 1,	//是否允许用户自定义金额
					'is_match_plan' => 1,	//是否自动匹配套餐
					'describe' => '1. 账户充值仅限微信在线方式支付，充值金额实时到账；
2. 账户充值套餐赠送的金额即时到账；
3. 账户余额有效期：自充值日起至用完即止；
4. 若有其它疑问，可拨打客服电话400-000-1234',	//充值说明
                ],
            ],
			'dealer' => [
                'key' => 'dealer',
                'describe' => '分销设置',
                'values' => [
                    'is_open' => 0,  //是否开启分销功能
                    'first_money' => 5,  //一级佣金比例
                    'second_money' => 5,  //二级佣金比例
                    'min_money' => 100,  //最低提现额度
                ],
            ],
            'poster' => [
                'key' => 'poster',
                'describe' => '海报设置',
                'values' => [
                    'backdrop' => [
                        'src' => web_url().'assets/store/img/dealer/poster.png',
                    ],
                    'nickName' => [
                        'fontSize' => 14,
                        'color' => '#FFFFFF',
                        'left' => 150,
                        'top' => 99,
                    ],
                    'avatar' => [
                        'width' => 70,
                        'style' => 'circle',
                        'left' => 150,
                        'top' => 18,
                    ],
                    'qrcode' => [
                        'width' => 160,
                        'style' => 'circle',
                        'left' => 108,
                        'top' => 190,
                    ],
					'shop' => [
						'shop_id' => '',
                        'shop_name' => '',
                        'address' => '',
                        'phone' => '',
                        'file_name' => '',
						'logo' => '',
                    ],
                ],
            ],
        ];
    }

}
